package com.atguigu.ggkt.exception;

/**
 * @author zfj
 * @date 2022/7/7 -21:50
 */

import com.atguigu.ggkt.response.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一异常处理类
 *
 * @ControllerAdvice 底层aop
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
	@ExceptionHandler(Exception.class)
	@ResponseBody
	public Result error(Exception e){
		e.printStackTrace();
		log.error("全局异常:{}",e.getStackTrace());
		return Result.fail().message("执行了全局异常处理");
	}

	//处理特定异常
	@ExceptionHandler(ArithmeticException.class)
	@ResponseBody
	public Result error(ArithmeticException e){
		e.printStackTrace();
		log.error("ArithmeticException特定异常:{}",e.getStackTrace());
		return Result.fail().message("执行了ArithmeticException特定异常处理");
	}


	//处理自定义异常
	@ExceptionHandler(GgktException.class)
	@ResponseBody
	public Result error(GgktException e){
		e.printStackTrace();
		log.error("执行GgktException自定义异常:{}",e.getStackTrace());
		return Result.fail().message(e.getMsg()).code(e.getCode());
	}
}
